const mysql = require("mysql");
// const { connect } = require("../app");

const pool = mysql.createPool({
    user: 'root',
    password: '123456',
    host: 'localhost',
    database: 'sushi',
    connectionLimit: 10
})

pool.query("select * from user_info", (err, result, fields) => {
    if (err) throw err;
    console.log(result);
})
pool.query("select * from goods", (err, result, fields) => {
    if (err) throw err;
    // console.log(result);
})
pool.query("select * from code", (err, result, fields) => {
    if (err) throw err;
    // console.log(result);
})
pool.query("select * from yweek", (err, result, fields) => {
    if (err) throw err;
    
})
pool.query("select * from Week", (err, result, fields) => {
    if (err) throw err;
   
})
pool.query("select * from month", (err, result, fields) => {
    if (err) throw err;
   
})
pool.query("select * from ymonth", (err, result, fields) => {
    if (err) throw err;
   
})
pool.query("select * from tmonth", (err, result, fields) => {
    if (err) throw err;
    
})
pool.query("select * from tweek", (err, result, fields) => {
    if (err) throw err;
   
})
pool.query("select * from yquarter", (err, result, fields) => {
    if (err) throw err;
    console.log(result);
})
pool.query("select * from quarters", (err, result, fields) => {
    if (err) throw err;
    console.log(result);
})
pool.query("select * from tquarter", (err, result, fields) => {
    if (err) throw err;
    console.log(result);
})


// 自主的管理连接
pool.getConnection((err, connection) => {
    if (err) throw err,
        connect.query("select * from user_info", (err, result) => {
            if (err) throw err;
            console.log(result);
            connection.release(); //释放连接
        })
})

pool.getConnection((err, connection) => {
    if (err) throw err,
        connect.query("select * from goods", (err, result) => {
            if (err) throw err;
            console.log(result);
            connection.release(); //释放连接
        })
})
pool.getConnection((err, connection) => {
    if (err) throw err,
        connect.query("select * from code", (err, result) => {
            if (err) throw err;
            console.log(result);
            connection.release(); //释放连接
        })
})

// promise控制对象
module.exports = (sql, params) => {
    return new Promise((resolve, reject) => {
        pool.getConnection((err, connection) => {
            if (err) reject(err);
            connection.query(sql, params, (err, result, fields) => {
                connection.release(); //释放链接
                if (err) reject(err);
                resolve(result);
            })
        })
    })
}